package com.ggh.jinjilive.ui.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentMineDetailBinding;
import com.ggh.jinjilive.databinding.FragmentTaskReceiveBinding;
import com.ggh.jinjilive.mobile.live.SellGoodsMobile;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.order.OrderViewModel;
import com.ggh.jinjilive.ui.mine.adapter.MineDetailAdapter;
import com.ggh.jinjilive.ui.mine.adapter.TaskReceiveAdapter;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TaskReceiveFragment extends BaseFragment<FragmentTaskReceiveBinding> implements OnRefreshListener, OnLoadMoreListener {
    //VM
    private TaskReceiveViewModel taskReceiveViewModel;
    //适配器
    private TaskReceiveAdapter taskReceiveAdapter;
    //区分
    private String particulars;

    public TaskReceiveFragment(String particulars){
        this.particulars = particulars;

    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        taskReceiveViewModel = new ViewModelProvider(this,viewModelFactory).get(TaskReceiveViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_task_receive;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        onRefresh(binding.refreshLayout);
    }


    @Override
    public void initView() {
        super.initView();
        taskReceiveAdapter = new TaskReceiveAdapter();
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(taskReceiveAdapter);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //是否启用上拉加载功能
        binding.refreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);

        //我的任务
        taskReceiveViewModel.mSellGoodsMobileList.observe(this, new Observer<List<SellGoodsMobile>>() {
            @Override
            public void onChanged(List<SellGoodsMobile> sellGoodsMobileList) {
                if(taskReceiveViewModel.isRefresh){
                    if(sellGoodsMobileList.size() > 0){
                        taskReceiveAdapter.setAdd(sellGoodsMobileList);
                        loadService.showSuccess();
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(sellGoodsMobileList.size() > 0){
                        taskReceiveAdapter.Add(sellGoodsMobileList);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        taskReceiveViewModel.isRefresh = true;
        if(particulars.equals("我的任务")){
            taskReceiveViewModel.myTaskList(0);
        }else if(particulars.equals("商家任务")){
            taskReceiveViewModel.TaskTaskList();
        }else if(particulars.equals("任务记录")){
            taskReceiveViewModel.myTaskList(1);
        }
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        taskReceiveViewModel.isRefresh = false;
        if(particulars.equals("我的任务")){
            taskReceiveViewModel.myTaskList(0);
        }else if(particulars.equals("商家任务")){
            taskReceiveViewModel.TaskTaskList();
        }else if(particulars.equals("任务记录")){
            taskReceiveViewModel.myTaskList(1);
        }
    }


}
